import 'dart:ui';

import 'package:development_assistant/config/colors.dart';
import 'package:development_assistant/config/font.dart';
import 'package:development_assistant/config/fonticon.dart';
import 'package:development_assistant/controller/api_controller.dart';
import 'package:development_assistant/model/api_info.dart';
import 'package:development_assistant/ui/screen/widget/frosted_glass.dart';
import 'package:development_assistant/ui/screen/widget/home_background.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../config/image_config.dart';
import '../widget/custom_text.dart';
class ApiAssistant extends StatelessWidget {
  ApiAssistant({super.key});

  final ApiController apiController=Get.put(ApiController());

  @override
  Widget build(BuildContext context) {
    return HomeBackground(
        front: SingleChildScrollView(
            child: Container(
              padding: EdgeInsets.symmetric(vertical: 50.h,horizontal: 20.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _requestType(),
                  SizedBox(height: 10.h,),
                  _apiManage(),
                ],
              ),
            )
        )
    );
  }

  _methodTag(String requestMethod,ApiInfo apiInfo){
    return SizedBox(
      child: TextButton(
          style: TextButton.styleFrom(
            foregroundColor: Colors.white,
            backgroundColor: ColorsConfig.methodTagColors[requestMethod],
            shape:RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10)
            )
          ),
          onPressed: (){
            apiController.apiID.value=apiInfo.apiID!;
            apiController.apiName.value=apiInfo.apiName;
            apiController.apiMethod.value=apiInfo.apiMethod;
            apiController.apiUrl.value=apiInfo.apiUrl;
            apiController.apiDate.value=apiInfo.apiDate;
            apiController.resultCode.value=0;
            apiController.result.value="";
            Get.toNamed("/EditApi");
          },
          child: Text(requestMethod)
      ),
    );
  }

  //接口管理
  _apiManage(){
    return FutureBuilder(
      future: apiController.getApiList(),
      builder: (context,snapshot) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text("接口管理".tr,style: FontConfig.titleStyle,),
            Obx(() => apiController.apiList.isEmpty?_apiFigure():Wrap(
              children: List<Widget>.generate(apiController.apiList.length, (index) {
                return Container(
                    margin: EdgeInsets.symmetric(vertical: 10.h),
                    padding: EdgeInsets.symmetric(vertical: 10.h,horizontal: 20.w),
                    decoration: BoxDecoration(
                        gradient: ColorsConfig().cardColorOfTheme,
                        borderRadius: BorderRadius.circular(20)
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            CustomText(text:apiController.apiList[index].apiName, textStyle: FontConfig.apiName),
                            _methodTag(apiController.apiList[index].apiMethod,apiController.apiList[index]),
                          ],
                        ),
                        CustomText(text: apiController.apiList[index].apiUrl, textStyle:  FontConfig.urlStyle),
                        CustomText(text: apiController.apiList[index].apiDate, textStyle:  FontConfig.urlStyle),
                      ],
                    )
                );
              }),
            ),)
          ],
        );
      }
    );
  }

  //请求方式
  _requestType(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        //快速请求
        Container(
          width: 150.w,
          height: 150.h,
          padding: EdgeInsets.only(top: 25.h),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20),
              gradient: ColorsConfig().requestTypeColorOfTheme
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              IconButton(
                splashColor: Colors.transparent,
                highlightColor: Colors.transparent,
                onPressed: (){
                  apiController.apiParams.removeRange(1, apiController.apiParams.length);
                  apiController.apiHeaders.removeRange(1, apiController.apiHeaders.length);
                  apiController.resultCode.value=0;
                  apiController.result.value="";
                  Get.toNamed("/QuickRequest");
                },
                icon: Icon(FontIcons.quickRequest,color: ColorsConfig().iconColorOfTheme,size: 70.w,),
              ),
              SizedBox(height: 5.h,),
              FrostedGlass(
                  width: 150.w,
                  height: 45.h,
                  color: Colors.white12,
                  borderRadius: BorderRadius.only(bottomLeft: Radius.circular(20),bottomRight: Radius.circular(20)),
                  sigmaX: 10, sigmaY: 10,
                  child: Text("快速请求".tr,style: FontConfig.apiTypeStyle,)
              )
            ],
          )
        ),
        SizedBox(width: 20.w,),
        //接口管理
        Container(
            width: 150.w,
            height: 150.h,
            padding: EdgeInsets.only(top: 25.h),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20),
                gradient: ColorsConfig().requestTypeColorOfTheme
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                IconButton(
                  splashColor: Colors.transparent,
                  highlightColor: Colors.transparent,
                  onPressed: (){
                    apiController.apiParams.removeRange(1, apiController.apiParams.length);
                    apiController.apiHeaders.removeRange(1, apiController.apiHeaders.length);
                    apiController.resultCode.value=0;
                    apiController.result.value="";
                    Get.toNamed("/ApiManage");
                  },
                  icon: Icon(FontIcons.requestManage,color: ColorsConfig().iconColorOfTheme,size: 70.w,),
                ),
                SizedBox(height: 5.h,),
                FrostedGlass(
                    width: 150.w,
                    height: 45.h,
                    color: Colors.white12,
                    borderRadius: BorderRadius.only(bottomLeft: Radius.circular(20),bottomRight: Radius.circular(20)),
                    sigmaX: 10, sigmaY: 10,
                    child: Text("接口管理".tr,style: FontConfig.apiTypeStyle,)
                )
              ],
            )
        ),
      ],
    );
  }

  _apiFigure(){
    return Container(
      height: 370.h,
      width: 320.w,
      margin: EdgeInsets.symmetric(vertical: 10.h),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15),
          gradient: ColorsConfig().figureOfTheme
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text("接口测试与管理".tr,style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 18.sp),),
          SizedBox(height: 5.h,),
          Text("对接口信息进行管理".tr,style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 11.sp)),
          Text("能够进行快速请求得到服务器数据".tr,style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 11.sp),),
          Image.asset(
            ImageConfig.figureImage['api']!,
            width: 250.w,
          ),
        ],
      ),
    );
  }
}
